//
//  HoomViewController.swift
//  sina----
//
//  Created by mac on 16/5/8.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
private let Hoomcell = "Hoomcell"
class HoomViewController: RootViewController {

    lazy var listArray = [Int]()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setRefresh()
        
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: Hoomcell)
        
        visitorView?.setupInfo(nil, lableText: "关注一些人，回这里看看有什么惊喜")
        //------
        automaticallyAdjustsScrollViewInsets = false
        
        var insets = tableView.contentInset
        insets.top = (navigationController?.navigationBar.bounds.height)! + 20
        
        tableView.contentInset = insets
        tableView.scrollIndicatorInsets = insets
        
        tableView.frame = CGRect(x: 0, y: 20, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height)
        //-------
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    override func loadData() {
    
        let defaultIndex = 100
        
        let isPushUp = pushUpView.isAnimating()
        if !isPushUp {
            
            refreshControl.beginRefreshing()
        }
        
        
        dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in
            
            NSThread.sleepForTimeInterval(2)
            
            let pushUpIndex = self.listArray.last ?? 0
            let pushDownIndex = self.listArray.first ?? defaultIndex
            
            var arr = [Int]()
            if isPushUp{
                
                for i in 0..<10 {
                    arr.append(pushUpIndex + i + 1)
                }
                
                self.listArray += arr
            } else {
            
                for i in (0..<10).reverse() {
                    arr.append(pushDownIndex - i - 1)
                }
                
                self.listArray = arr + self.listArray
            }
            
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                //关闭刷新
                self.refreshControl.endRefreshing()
                self.pushUpView.stopAnimating()
                
                self.tableView.reloadData()
            })
        }
        
    }
    
    

}
// MARK: - 代理, 数据源方法
extension HoomViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return listArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(Hoomcell, forIndexPath: indexPath)
        cell.textLabel?.text = listArray[indexPath.row].description + "-\(indexPath.row)"
        
        return cell
    }
    //MARK: - cell左滑出现删除按钮
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        
        
    }
    //MARK: - 修改左滑出现的文字, 及注册按钮事件
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        
        let action: UITableViewRowAction = UITableViewRowAction.init(style: .Default, title: "删除") { (_ , _ ) -> Void in
            
            
        }
        let action1: UITableViewRowAction = UITableViewRowAction.init(style: .Normal, title: "编辑") { (UITableViewRowAction, NSIndexPath) -> Void in
            
            
        }
        
        action1.backgroundColor = UIColor.blueColor()
        
        return [action, action1]
    }
    //MARK: - 上下滑动view, 导航栏隐藏或出现
    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        
        if velocity.y > 0 {
            navigationController?.setNavigationBarHidden(true, animated: true)
        } else {
            navigationController?.setNavigationBarHidden(false, animated: true)
        }
    }
}
